"""云服务提供商接口"""

import random
from datetime import datetime, timedelta
from typing import Dict, List, Any
from app.models.resources import ECSInstance, OSSBucket, RDSInstance, SLBInstance
from app.models.monitor import MetricData, NetworkMetrics, MonitorData

class MockCloudProvider:
    """模拟云服务提供商"""
    
    def __init__(self, provider: str, access_key: str, secret_key: str):
        self.provider = provider
        self.access_key = access_key
        self.secret_key = secret_key
    
    def get_ecs_instances(self, region: str = None) -> List[ECSInstance]:
        """获取云服务器列表"""
        instances = []
        
        # 模拟数据
        regions = [region] if region else ['cn-hangzhou', 'cn-shanghai', 'cn-beijing']
        
        for r in regions:
            for i in range(1, 4):
                instances.append(ECSInstance(
                    id=f"i-{random.randint(100000, 999999)}",
                    name=f"服务器-{i}",
                    instance_type="ecs.c5.large",
                    status=random.choice(["Running", "Stopped", "Pending"]),
                    public_ip=f"47.100.{random.randint(1, 255)}.{random.randint(1, 255)}",
                    private_ip=f"192.168.{random.randint(1, 255)}.{random.randint(1, 255)}",
                    region=r,
                    zone=f"{r}-a",
                    created_at=datetime.now() - timedelta(days=random.randint(1, 365))
                ))
        
        return instances
    
    def get_oss_buckets(self, region: str = None) -> List[OSSBucket]:
        """获取对象存储桶列表"""
        buckets = []
        
        regions = [region] if region else ['cn-hangzhou', 'cn-shanghai', 'cn-beijing']
        
        for r in regions:
            for i in range(1, 3):
                buckets.append(OSSBucket(
                    name=f"my-bucket-{i}-{random.randint(1000, 9999)}",
                    region=r,
                    storage_class=random.choice(["Standard", "IA", "Archive"]),
                    created_at=datetime.now() - timedelta(days=random.randint(1, 365)),
                    size=random.randint(1000000, 1000000000)
                ))
        
        return buckets
    
    def get_rds_instances(self, region: str = None) -> List[RDSInstance]:
        """获取数据库实例列表"""
        instances = []
        
        regions = [region] if region else ['cn-hangzhou', 'cn-shanghai', 'cn-beijing']
        
        for r in regions:
            for i in range(1, 3):
                instances.append(RDSInstance(
                    id=f"rds-{random.randint(100000, 999999)}",
                    name=f"数据库-{i}",
                    engine=random.choice(["MySQL", "PostgreSQL", "SQLServer"]),
                    engine_version="8.0",
                    status=random.choice(["Running", "Stopped"]),
                    region=r,
                    created_at=datetime.now() - timedelta(days=random.randint(1, 365))
                ))
        
        return instances
    
    def get_slb_instances(self, region: str = None) -> List[SLBInstance]:
        """获取负载均衡实例列表"""
        instances = []
        
        regions = [region] if region else ['cn-hangzhou', 'cn-shanghai', 'cn-beijing']
        
        for r in regions:
            for i in range(1, 2):
                instances.append(SLBInstance(
                    id=f"lb-{random.randint(100000, 999999)}",
                    name=f"负载均衡-{i}",
                    status="Active",
                    type="public",
                    region=r,
                    address=f"47.100.{random.randint(1, 255)}.{random.randint(1, 255)}"
                ))
        
        return instances
    
    def get_monitor_data(self, instance_id: str, period: int = 3600) -> MonitorData:
        """获取监控数据"""
        # 生成模拟监控数据
        labels = []
        cpu_values = []
        memory_values = []
        network_in_values = []
        network_out_values = []
        
        now = datetime.now()
        for i in range(12):  # 过去1小时，每5分钟一个点
            time = now - timedelta(minutes=(11-i)*5)
            labels.append(time.strftime("%H:%M"))
            
            # 生成随机监控数据
            cpu_values.append(random.uniform(10, 80))
            memory_values.append(random.uniform(30, 70))
            network_in_values.append(random.uniform(100, 1000))
            network_out_values.append(random.uniform(200, 800))
        
        return MonitorData(
            cpu=MetricData(labels=labels, values=cpu_values),
            memory=MetricData(labels=labels, values=memory_values),
            network=NetworkMetrics(
                labels=labels,
                in_values=network_in_values,
                out_values=network_out_values
            )
        )

def get_cloud_provider(provider: str, access_key: str, secret_key: str) -> MockCloudProvider:
    """获取云服务提供商实例"""
    return MockCloudProvider(provider, access_key, secret_key)
